import { MES_SMH_ApiHost } from '@/utils/Interceptors'

// 工序详情（与后端 WorkProcessDetailDTO 对齐）
export interface WorkProcessDetail {
  workProcessId: number
  processCode?: string
  processName?: string
  workRatio?: number
  workPermission?: string
}

// 工单数据类型 - 根据后端DTO定义
export interface WorkOrder {
  workOrderId: number
  productId?: number
  orderNo?: string
  taskName?: string
  // 新增：强关联与冗余字段
  workProcessId?: number
  processCode?: string
  workPermission?: string
  // 详情返回的工序详情
  workProcessDetail?: WorkProcessDetail | null
  product?: string
  productCode?: string
  productName?: string
  productSpec?: string
  relatedDocuments?: string
  plannedStartTime?: string
  plannedEndTime?: string
  plannedQty?: number
  actualQty?: number
  goodQty?: number
  badQty?: number
  workTimeMinutes?: number
  producer?: string
  workCount?: number
  progress?: string
  startTime?: string
  endTime?: string
  workTimeHours?: number
  standardEfficiency?: number
  actualEfficiency?: number
  approveStatus?: string
  approver?: string
  approveTime?: string
  unit?: string
  status?: string
  remarks?: string
  createTime?: string
  creator?: string
  modifier?: string
  modifyTime?: string
}

// API响应类型
export interface ApiResponse<T> {
  code: number
  msg: string
  data: T
}

// 分页响应类型 - 根据后端DTO更新
export interface PagedResponse<T> {
  totalCount: number
  items: T[]
  pageIndex: number
  pageSize: number
  totalPages: number
}

// 搜索参数类型 - 根据后端DTO更新
export interface SearchParams {
  orderNo?: string
  pageIndex?: number
  pageSize?: number
}

// 获取工单分页列表 - 根据后端API更新
export const getWorkOrders = async (params: SearchParams) => {
  return await MES_SMH_ApiHost.post<ApiResponse<PagedResponse<WorkOrder>>>('/api/WorkOrder/GetPageList', params)
}

// 获取工单详情
export const getWorkOrderById = async (id: number) => {
  return await MES_SMH_ApiHost.get<ApiResponse<WorkOrder>>(`/api/WorkOrder/GetById/${id}`)
}

// 创建工单
export const createWorkOrder = async (data: Partial<WorkOrder>) => {
  return await MES_SMH_ApiHost.post<ApiResponse<WorkOrder>>('/api/WorkOrder/Create', {
    createDto: data
  })
}

// 更新工单
export const updateWorkOrder = async (id: number, data: Partial<WorkOrder>) => {
  return await MES_SMH_ApiHost.put<ApiResponse<WorkOrder>>(`/api/WorkOrder/Update/${id}`, data)
}

// 删除工单
export const deleteWorkOrder = async (id: number) => {
  return await MES_SMH_ApiHost.delete<ApiResponse<boolean>>(`/api/WorkOrder/Delete/${id}`)
}

// 批量删除工单
export const batchDeleteWorkOrders = async (ids: number[]) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>('/api/WorkOrder/BatchDelete', ids)
}

// 开始工单
export const startWorkOrder = async (id: number) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>(`/api/WorkOrder/Start/${id}`)
}

// 结束工单
export const endWorkOrder = async (id: number) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>(`/api/WorkOrder/End/${id}`)
}

// 取消工单
export const cancelWorkOrder = async (id: number) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>(`/api/WorkOrder/Cancel/${id}`)
}

// 撤回工单
export const recallWorkOrder = async (id: number) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>(`/api/WorkOrder/Recall/${id}`)
}

// 产品相关接口
export interface Product {
  productId: number
  productCode: string
  productName: string
  productSpec?: string
  inventoryUnit: string
  productAttribute: string
  processRouteId?: number
  maxInventory?: number
  minInventory?: number
  safetyInventory?: number
  currentInventory?: number
  productImage?: string
  createTime?: string
  creator?: string
  modifyTime?: string
  modifier?: string
}

// 工序相关接口
export interface WorkProcess {
  workProcessId: number
  processCode: string
  processName: string
  workRatio?: number
  workPermission?: string
  createTime?: string
  creator?: string
  modifyTime?: string
  modifier?: string
}

// 获取产品列表
export const getProducts = async () => {
  return await MES_SMH_ApiHost.get<ApiResponse<Product[]>>('/api/WorkOrder/GetProducts')
}

// 根据产品ID获取产品详情
export const getProductById = async (productId: number) => {
  return await MES_SMH_ApiHost.get<ApiResponse<Product>>(`/api/WorkOrder/GetProductById/${productId}`)
}

// 获取工序列表
export const getWorkProcesses = async () => {
  return await MES_SMH_ApiHost.get<ApiResponse<WorkProcess[]>>('/api/WorkOrder/GetWorkProcesses')
}

// 根据产品ID获取相关工序列表
export const getWorkProcessesByProductId = async (productId: number) => {
  return await MES_SMH_ApiHost.get<ApiResponse<WorkProcess[]>>(`/api/WorkOrder/GetWorkProcessesByProductId/${productId}`)
}

// 导出工单数据
export const exportWorkOrders = async (params?: SearchParams) => {
  return await MES_SMH_ApiHost.get('/api/WorkOrder/Export', { 
    params,
    responseType: 'blob',
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, */*'
    }
  })
}
